/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
 * * * * * * * * * * * array  数组处理方法 * * * * * * * * * * * * *
 * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */


/**
 * 递归处理多维数组
 * @param arr_data
 * @param callback
 * @param sub_key
 * @return {*[]}
 */
export function infiniteErgodic(arr_data: Array<{ [key: string]: any }>, callback: (obj: any) => any, sub_key: string = 'children'): Array<{ [key: string]: any }> {
    let arr: Array<{ [key: string]: any }> = [];
    for (let index in arr_data) {
        const tmp = {...arr_data[index]};
        if (tmp[sub_key]) {
            tmp[sub_key] = infiniteErgodic(tmp[sub_key], callback, sub_key);
        }
        arr.push(callback(tmp));
    }
    return arr;
}


/**
 * 插入数据
 */
export function InsertData(data: Array<any>, val: any, index: number) {
    if (data.length === 0) return [val]
    if (typeof data[index] === 'undefined') {
        data.push(val)
        return data
    }
    let newsData: any[] = []
    for (let sort in data) {
        if (parseInt(sort) === index) {
            newsData.push(val)
        }
        newsData.push(data[sort])
    }
    return newsData
}


/**
 * 数组分组
 * @param arr
 * @param groupSize
 */
export function chunkArray(arr: any[], groupSize: number) {
    let result = [];
    for (let i = 0; i < arr.length; i += groupSize) {
        let chunk = arr.slice(i, i + groupSize);
        result.push(chunk);
    }
    return result;
}